/*
** runProc.c for runProc in /home/vermel/local/epitech/tek2/c/ftrace/pullup-project
** 
** Made by nathan vermelle
** Login   <vermel_n@epitech.net>
** 
** Started on  Fri Jun 29 14:07:42 2012 nathan vermelle
** Last update Sun Jul  1 17:40:04 2012 nathan vermelle
*/

#include "./../includes/ftrace.h"

void		runChildProc(pid_t pid, char *cmd)
{
   if ((ptrace(PTRACE_TRACEME, pid, NULL, NULL)) < 0)
      merror("Ptrace - TRACEME failed");
   if ((execlp(cmd, cmd, NULL)) < 0)
      merror("exelp failed");
}

void		runChildProcPid(pid_t pid)
{
   if ((ptrace(PTRACE_TRACEME, pid, NULL, NULL)) < 0)
      merror("Ptrace - TRACEME failed");
}

void		runFatherProc(pid_t pid)
{
   int				status = 0;
   long				_rax = 0;
   struct rusage		r;
   struct user_regs_struct	regs;

   while (42)
   {
      wait4(pid, &status, 0, &r);
      if (WIFEXITED(status))
      {
	 printf("%s", get_syscall(230));
	 exit(-1);
      }
      if ((_rax = ptrace(PTRACE_GETREGS, pid , NULL, &regs)) < 0)
	 merror("ptrace - request : PTRACE_GETREGS");
      ptrace(PTRACE_PEEKTEXT, pid , regs.rip , NULL);
      _rax = ptrace(PTRACE_PEEKUSER, pid, ORIG_RAX * 8, NULL);
      if ((_rax & 0x0000ffff) == 0x80)
         printf("%s", get_syscall(regs.rax));
      else if (get_syscall(_rax))
         printf("%s", get_syscall(_rax));
      if ((ptrace(PTRACE_SINGLESTEP, pid, NULL, NULL)) < 0)
         merror("ptrace - request : PTRACE_SINGLESTEP");
   }
}
